﻿using AutoMapper;
using CSRedis;
using EMS.system.Common;
using EMS.system.Entityies.BaseData;
using EMS.system.Entityies.MasterData.CustomerManagementModel;
using EMS.system.IEntityAppService.MasterData.CustomerManagementIService;
using EMS.system.IEntityAppService.MasterData.CustomerManagementIService.CustomerManagementDTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace EMS.system.EntityAppService.MasterData.CustomerManagementService
{
    public class CustomerManagementAppService : ApplicationService, ICustomerManagementAppService
    {
        private readonly IRepository<CustomerManagement, int> repository;
        private readonly IRepository<DictItem, int> dicRepository;
        private static object obj = new object();
        private readonly CSRedisClient _client = new CSRedisClient("localhost:6379");

        public CustomerManagementAppService(IRepository<CustomerManagement, int> repository, IRepository<DictItem, int> dicRepository)
        {
            this.dicRepository = dicRepository;
            this.repository = repository;
        }

        public async Task<int> AddCustomerManagement(CustomerManagementInsertDTO dto)
        {
            //var customer = ObjectMapper.Map<CustomerManagementInsertDTO, CustomerManagement>(dto);
            var customer = dto.MapTo<CustomerManagement>();
            await repository.InsertAsync(customer);
            return 1;
        }

        public PagedResult GetCustomerManagement(string? code, string? name, string? forshort, int page = 1, int limit = 2)
        {
            var predicate = PredicateBuilder.New<CustomerManagement>(true);
            if (!string.IsNullOrEmpty(code))
            {
                predicate = predicate.And(m => m.CustomerCode == code);
            }
            if (!string.IsNullOrEmpty(name))
            {
                predicate = predicate.And(m => m.CustomerName == name);
            }
            if (!string.IsNullOrEmpty(forshort))
            {
                predicate = predicate.And(m => m.CustomerAbbreviation == forshort);
            }

            var dto = repository.GetListAsync(predicate).Result;
            PagedResult result = new PagedResult();
            result.PageCount = (int)Math.Ceiling((decimal)(dto.Count == 0 ? 1 : dto.Count) / limit);
            result.PageSize = limit;
            result.CurrentPage = page;
            result.RowCount = dto.Count;
            result.Queryable = dto.Skip((page - 1) * limit).Take(limit).AsQueryable();
            return result;

        }


        public int DeleteCustomerManagement(int id)
        {
            repository.DeleteAsync(id);
            return 1;
        }

        public CustomerManagement GetCustomerById(int id)
        {
            var model = repository.FindAsync(x => x.Id == id).Result;
            return model;
        }
        public async Task<int> UpdateCustomer(CustomerManagementUpdateDTO dto)
        {
            //var model = ObjectMapper.Map<CustomerManagementUpdateDTO, CustomerManagement>(dto);
            var model = dto.MapTo<CustomerManagement>();
            await repository.UpdateAsync(model);
            return 1;
        }

        //public List<DictItem> GetDictItem(int id = 1)
        //{
        //    var list = dicRepository.GetListAsync(x => x.DictTypeId == id).Result;
        //    return list;
        //}

        public async Task<List<DictItem>> GetDictItem(int id=22)
        {
            //根据Key值，获取学生list集合
            var dictItemList = EMS.system.Common.RedisHelper.Get<List<DictItem>>("dictItemList");
            if (dictItemList == null)
            {
                lock (obj)
                {
                    if (dictItemList == null)
                    {
                        //从数据库中取出学生集合
                        dictItemList = dicRepository.GetListAsync(x => x.DictTypeId == id).Result;
                        var seconds = 30000 + new Random().Next(1, 1000);
                        //将学生集合缓存在Redis中，key值为：StudentList
                        EMS.system.Common.RedisHelper.Set("dictItemList", dictItemList, DateTime.Now.AddDays(1));
                    }
                }
            }
            return dictItemList;
        }

    }
}
